﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RecipeManager
{
    public partial class RecipeManagerForm : Form
    {
        #region Recipe List

        private List<Recipe> _recipeList = new List<Recipe>();

        public List<Recipe> RecipeList
        {
            get { return _recipeList; }
        }

        #endregion

        #region Construktor

        public RecipeManagerForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Close

        private void _menMainClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region About Box

        private void _menHelpVersion_Click(object sender, EventArgs e)
        {
            new AboutForm().ShowDialog(this);
        }

        #endregion

        #region Load Recipe List

        public void LoadRecipes()
        {
            List<int> recipeDataList = Recipe.GetRecipiesInRepository();

            RecipeList.Clear();
            //    public static List<Recipe> LoadAllRecipies()
            //{
            //    return
            //        GetRecipiesInRepository().ConvertAll<Recipe>(x => Load(x));
            //}
            LoadProgressForm loadProgressForm = null;
            if (loadProgressForm == null)
            {
                loadProgressForm = new LoadProgressForm();
                loadProgressForm.Show();
                loadProgressForm.TopMost = true;


                loadProgressForm.Count = recipeDataList.Count;
                loadProgressForm.Value = 0;
                for (int i = 0; i < recipeDataList.Count; i++)
                {

                    RecipeList.Add(Recipe.Load(recipeDataList[i]));

                    loadProgressForm.Value = i + 1;
                }
            }

            RecipeBinding.DataSource = RecipeList;
        }

        #endregion

        private void Filter_TextChanged(object sender, EventArgs e)
        {
            RefreshDataSource();
        }

        public void RefreshDataSource()
        {
            RecipeList.Sort((x, y) => x.UniqueRecipeID - y.UniqueRecipeID);

            FilterRows();
        }

        public void FilterRows()
        {
            //RecipeBinding.DataSource = RecipeList
            //    .Where(recept => recept.Name.Contains(filterName))
            //    .Where(recept => recept.CategoryValues.Contains(filterCategory))
            //    .Where(recept => recept.IngredientListValues.Contains(filterIngredients));

            var list = RecipeList.ToList();

            if (_filterForm != null)
            {
                foreach (var item in _filterForm.FilterList)
                {
                    switch (item.FilterType)
                    {
                        case "Name":
                            list = list.Where(recipe => recipe.Name.ToUpper().Contains((item.Value ?? "").ToUpper())).ToList();
                            break;
                        case "Kategorie":
                            list = list.Where(recipe => recipe.CategoryValues.ToUpper().Contains((item.Value ?? "").ToUpper())).ToList();
                            break;
                        case "Zutat":
                            list = list.Where(recipe => recipe.IngredientListValues.ToUpper().Contains((item.Value ?? "").ToUpper())).ToList();
                            break;
                    }
                }
            }

            RecipeBinding.DataSource = list;
        }

        #region Functions

        #region New Recipe

        private void _butNewRecipe_Click(object sender, EventArgs e)
        {
            Recipe recipe = new Recipe();
            recipe.UniqueRecipeID = 1;

            if (RecipeList.Count > 0)
            {
                recipe.UniqueRecipeID = RecipeList.Max(x => x.UniqueRecipeID) + 1;
            }

            if (new RecipeForm(recipe).ShowDialog(this) == DialogResult.OK)
            {
                RecipeList.Add(recipe);
                recipe.Save();
            }
            RefreshDataSource();
        }

        #endregion

        #region Edit Recipe

        private void _butEditRecipe_Click(object sender, EventArgs e)
        {
            Recipe recipe = (RecipeBinding.Current as Recipe).Clone() as Recipe;
            int recipeID = recipe.UniqueRecipeID;
            if (new RecipeForm(recipe).ShowDialog(this) == DialogResult.OK)
            {
                RecipeList.RemoveAll(x => x.UniqueRecipeID == recipeID);
                RecipeList.Add(recipe);
                recipe.Save();
            }
            RefreshDataSource();
        }

        #endregion

        private void _butDeleteRecipe_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Wollen Sie das Rezept löschen?", "Löschen", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
            {
                return;
            }

            int recipeID = (RecipeBinding.Current as Recipe).UniqueRecipeID;
            RecipeList.Where(x => x.UniqueRecipeID == recipeID).ToList().ForEach(x => x.Delete());
            RecipeList.RemoveAll(x =>
            {
                return x.UniqueRecipeID == recipeID;
            });
            RefreshDataSource();
        }

        #endregion

        private void _cmsGrid_Opening(object sender, CancelEventArgs e)
        {
            _cmsGridEdit.Enabled = RecipeList.Count > 0;
            _cmsGridDelete.Enabled = RecipeList.Count > 0;
        }

        private void _gridRecipeList_DoubleClick(object sender, EventArgs e)
        {
            if (RecipeList.Count > 0)
            {
                _butEditRecipe_Click(sender, e);
            }
        }

        FilterForm _filterForm;
        private void _btnFilterRecipe_Click(object sender, EventArgs e)
        {
            if (_filterForm != null)
            {
                _filterForm.Close();
                _filterForm = null;
                return;
            }
            _filterForm = new FilterForm();
            _filterForm.Show(this);
            _filterForm.FormClosed += (x, y) => { _filterForm = null; RefreshDataSource(); };
            _filterForm.FilterChanged += (x, y) => RefreshDataSource();
            _filterForm.LocationChanged += (x, y) => MoveFilterForm();
            MoveFilterForm();
        }

        private void RecipeManagerForm_LocationChanged(object sender, EventArgs e)
        {
            MoveFilterForm();
        }

        public void MoveFilterForm()
        {
            if (_filterForm == null)
            {
                return;
            }
            if (WindowState == FormWindowState.Normal)
            {
                _filterForm.Top = this.Top + 5;
                _filterForm.Left = this.Left + this.Width;
                _filterForm.Height = this.Height - 10;
            }
        }

        private void RecipeManagerForm_Resize(object sender, EventArgs e)
        {
            MoveFilterForm();
        }

        private void _menEditRefresh_Click(object sender, EventArgs e)
        {
            RefreshDataSource();
        }

        private void _mnuFileRereadRecipe_Click(object sender, EventArgs e)
        {
            LoadRecipes();
        }

        private void _btnShowReport_Click(object sender, EventArgs e)
        {
            new ReportForm(RecipeBinding.Current as Recipe).ShowDialog(this);
        }

        private void _btnShowAllReports_Click(object sender, EventArgs e)
        {
            new ReportForm((List<Recipe>)RecipeBinding.DataSource).ShowDialog(this);
        }

        private void _cmsGridOpenFolder_Click(object sender, EventArgs e)
        {
            (RecipeBinding.Current as Recipe).OpenFolder();
        }
            

        

    }
}
